<span style='font-size:11px; color:#000000; background:#ffffff; '><pre>
<span style='color:#800000; font-weight:bold; '>protected</span> <span style='color:#800000; font-weight:bold; '>void</span> convertToPdfButton_Click<span style='color:#808030; '>(</span><span style='color:#800000; font-weight:bold; '>object</span> sender<span style='color:#808030; '>,</span> EventArgs e<span style='color:#808030; '>)</span>
<span style='color:#800080; '>{</span>
    <span style='color:#696969; '>// Save variables in Session object</span>
    Session<span style='color:#808030; '>[</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>firstName</span><span style='color:#800000; '>"</span><span style='color:#808030; '>]</span> <span style='color:#808030; '>=</span> firstNameTextBox<span style='color:#808030; '>.</span>Text<span style='color:#800080; '>;</span>
    Session<span style='color:#808030; '>[</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>lastName</span><span style='color:#800000; '>"</span><span style='color:#808030; '>]</span> <span style='color:#808030; '>=</span> lastNameTextBox<span style='color:#808030; '>.</span>Text<span style='color:#800080; '>;</span>
    Session<span style='color:#808030; '>[</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>gender</span><span style='color:#800000; '>"</span><span style='color:#808030; '>]</span> <span style='color:#808030; '>=</span> maleRadioButton<span style='color:#808030; '>.</span>Checked ? <span style='color:#800000; '>"</span><span style='color:#0000e6; '>Male</span><span style='color:#800000; '>"</span> <span style='color:#808030; '>:</span> <span style='color:#800000; '>"</span><span style='color:#0000e6; '>Female</span><span style='color:#800000; '>"</span><span style='color:#800080; '>;</span>
    Session<span style='color:#808030; '>[</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>haveCar</span><span style='color:#800000; '>"</span><span style='color:#808030; '>]</span> <span style='color:#808030; '>=</span> haveCarCheckBox<span style='color:#808030; '>.</span>Checked<span style='color:#800080; '>;</span>
    Session<span style='color:#808030; '>[</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>carType</span><span style='color:#800000; '>"</span><span style='color:#808030; '>]</span> <span style='color:#808030; '>=</span> carTypeDropDownList<span style='color:#808030; '>.</span>SelectedValue<span style='color:#800080; '>;</span>
    Session<span style='color:#808030; '>[</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>comments</span><span style='color:#800000; '>"</span><span style='color:#808030; '>]</span> <span style='color:#808030; '>=</span> commentsTextBox<span style='color:#808030; '>.</span>Text<span style='color:#800080; '>;</span>

    <span style='color:#696969; '>// Execute the Display_Session_Variables.aspx page and get the HTML string </span>
    <span style='color:#696969; '>// rendered by this page</span>
    TextWriter outTextWriter <span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>new</span> StringWriter<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
    Server<span style='color:#808030; '>.</span>Execute<span style='color:#808030; '>(</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>Display_Session_Variables.aspx</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> outTextWriter<span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

    <span style='color:#800000; font-weight:bold; '>string</span> htmlStringToConvert <span style='color:#808030; '>=</span> outTextWriter<span style='color:#808030; '>.</span>ToString<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

    <span style='color:#696969; '>// Create a HTML to PDF converter object with default settings</span>
    HtmlToPdfConverter htmlToPdfConverter <span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>new</span> HtmlToPdfConverter<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

    <span style='color:#696969; '>// Set license key received after purchase to use the converter in licensed mode</span>
    <span style='color:#696969; '>// Leave it not set to use the converter in demo mode</span>
    htmlToPdfConverter<span style='color:#808030; '>.</span>LicenseKey <span style='color:#808030; '>=</span> <span style='color:#800000; '>"</span><span style='color:#0000e6; '>4W9+bn19bn5ue2B+bn1/YH98YHd3d3c=</span><span style='color:#800000; '>"</span><span style='color:#800080; '>;</span>

    <span style='color:#696969; '>// Set an adddional delay in seconds to wait for JavaScript or AJAX calls after page load completed</span>
    <span style='color:#696969; '>// Set this property to 0 if you don't need to wait for such asynchcronous operations to finish</span>
    htmlToPdfConverter<span style='color:#808030; '>.</span>ConversionDelay <span style='color:#808030; '>=</span> <span style='color:#008c00; '>2</span><span style='color:#800080; '>;</span>

    <span style='color:#696969; '>// Use the current page URL as base URL</span>
    <span style='color:#800000; font-weight:bold; '>string</span> baseUrl <span style='color:#808030; '>=</span> HttpContext<span style='color:#808030; '>.</span>Current<span style='color:#808030; '>.</span>Request<span style='color:#808030; '>.</span>Url<span style='color:#808030; '>.</span>AbsoluteUri<span style='color:#800080; '>;</span>

    <span style='color:#696969; '>// Convert the page HTML string to a PDF document in a memory buffer</span>
    <span style='color:#800000; font-weight:bold; '>byte</span><span style='color:#808030; '>[</span><span style='color:#808030; '>]</span> outPdfBuffer <span style='color:#808030; '>=</span> htmlToPdfConverter<span style='color:#808030; '>.</span>ConvertHtml<span style='color:#808030; '>(</span>htmlStringToConvert<span style='color:#808030; '>,</span> baseUrl<span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

    <span style='color:#696969; '>// Send the PDF as response to browser</span>

    <span style='color:#696969; '>// Set response content type</span>
    Response<span style='color:#808030; '>.</span>AddHeader<span style='color:#808030; '>(</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>Content-Type</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> <span style='color:#800000; '>"</span><span style='color:#0000e6; '>application/pdf</span><span style='color:#800000; '>"</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

    <span style='color:#696969; '>// Instruct the browser to open the PDF file as an attachment or inline</span>
    Response<span style='color:#808030; '>.</span>AddHeader<span style='color:#808030; '>(</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>Content-Disposition</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> String<span style='color:#808030; '>.</span>Format<span style='color:#808030; '>(</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>attachment; filename=Convert_Page_in_Same_Session.pdf; size={0}</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> outPdfBuffer<span style='color:#808030; '>.</span>Length<span style='color:#808030; '>.</span>ToString<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#808030; '>)</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

    <span style='color:#696969; '>// Write the PDF document buffer to HTTP response</span>
    Response<span style='color:#808030; '>.</span>BinaryWrite<span style='color:#808030; '>(</span>outPdfBuffer<span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

    <span style='color:#696969; '>// End the HTTP response and stop the current page processing</span>
    Response<span style='color:#808030; '>.</span>End<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
<span style='color:#800080; '>}</span>
</pre>
</span>
<br />
<br />
<b>Display Session Variables in Converted HTML Page</b><br />
<br />
<span style='font-size:11px; color:#000000; background:#ffffff; '><pre>
<span style='color:#800000; font-weight:bold; '>protected</span> <span style='color:#800000; font-weight:bold; '>void</span> Page_Load<span style='color:#808030; '>(</span><span style='color:#800000; font-weight:bold; '>object</span> sender<span style='color:#808030; '>,</span> EventArgs e<span style='color:#808030; '>)</span>
<span style='color:#800080; '>{</span>
    <span style='color:#800000; font-weight:bold; '>if</span> <span style='color:#808030; '>(</span><span style='color:#808030; '>!</span>IsPostBack<span style='color:#808030; '>)</span>
    <span style='color:#800080; '>{</span>
        firstNameLabel<span style='color:#808030; '>.</span>Text <span style='color:#808030; '>=</span> Session<span style='color:#808030; '>[</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>firstName</span><span style='color:#800000; '>"</span><span style='color:#808030; '>]</span> <span style='color:#808030; '>!</span><span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>null</span> ? <span style='color:#808030; '>(</span>String<span style='color:#808030; '>)</span>Session<span style='color:#808030; '>[</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>firstName</span><span style='color:#800000; '>"</span><span style='color:#808030; '>]</span> <span style='color:#808030; '>:</span> String<span style='color:#808030; '>.</span>Empty<span style='color:#800080; '>;</span>
        lastNameLabel<span style='color:#808030; '>.</span>Text <span style='color:#808030; '>=</span> Session<span style='color:#808030; '>[</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>lastName</span><span style='color:#800000; '>"</span><span style='color:#808030; '>]</span> <span style='color:#808030; '>!</span><span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>null</span> ? <span style='color:#808030; '>(</span>String<span style='color:#808030; '>)</span>Session<span style='color:#808030; '>[</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>lastName</span><span style='color:#800000; '>"</span><span style='color:#808030; '>]</span> <span style='color:#808030; '>:</span> String<span style='color:#808030; '>.</span>Empty<span style='color:#800080; '>;</span>
        genderLabel<span style='color:#808030; '>.</span>Text <span style='color:#808030; '>=</span> Session<span style='color:#808030; '>[</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>gender</span><span style='color:#800000; '>"</span><span style='color:#808030; '>]</span> <span style='color:#808030; '>!</span><span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>null</span> ? <span style='color:#808030; '>(</span>String<span style='color:#808030; '>)</span>Session<span style='color:#808030; '>[</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>gender</span><span style='color:#800000; '>"</span><span style='color:#808030; '>]</span> <span style='color:#808030; '>:</span> String<span style='color:#808030; '>.</span>Empty<span style='color:#800080; '>;</span>

        <span style='color:#800000; font-weight:bold; '>bool</span> iHaveCar <span style='color:#808030; '>=</span> Session<span style='color:#808030; '>[</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>haveCar</span><span style='color:#800000; '>"</span><span style='color:#808030; '>]</span> <span style='color:#808030; '>!</span><span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>null</span> ? <span style='color:#808030; '>(</span><span style='color:#800000; font-weight:bold; '>bool</span><span style='color:#808030; '>)</span>Session<span style='color:#808030; '>[</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>haveCar</span><span style='color:#800000; '>"</span><span style='color:#808030; '>]</span> <span style='color:#808030; '>:</span> <span style='color:#800000; font-weight:bold; '>false</span><span style='color:#800080; '>;</span>
        haveCarLabel<span style='color:#808030; '>.</span>Text <span style='color:#808030; '>=</span> iHaveCar ? <span style='color:#800000; '>"</span><span style='color:#0000e6; '>Yes</span><span style='color:#800000; '>"</span> <span style='color:#808030; '>:</span> <span style='color:#800000; '>"</span><span style='color:#0000e6; '>No</span><span style='color:#800000; '>"</span><span style='color:#800080; '>;</span>
        carTypePanel<span style='color:#808030; '>.</span>Visible <span style='color:#808030; '>=</span> iHaveCar<span style='color:#800080; '>;</span>
        carTypeLabel<span style='color:#808030; '>.</span>Text <span style='color:#808030; '>=</span> iHaveCar &amp;&amp; Session<span style='color:#808030; '>[</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>carType</span><span style='color:#800000; '>"</span><span style='color:#808030; '>]</span> <span style='color:#808030; '>!</span><span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>null</span> ? <span style='color:#808030; '>(</span>String<span style='color:#808030; '>)</span>Session<span style='color:#808030; '>[</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>carType</span><span style='color:#800000; '>"</span><span style='color:#808030; '>]</span> <span style='color:#808030; '>:</span> String<span style='color:#808030; '>.</span>Empty<span style='color:#800080; '>;</span>

        commentsLabel<span style='color:#808030; '>.</span>Text <span style='color:#808030; '>=</span> Session<span style='color:#808030; '>[</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>comments</span><span style='color:#800000; '>"</span><span style='color:#808030; '>]</span> <span style='color:#808030; '>!</span><span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>null</span> ? <span style='color:#808030; '>(</span>String<span style='color:#808030; '>)</span>Session<span style='color:#808030; '>[</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>comments</span><span style='color:#800000; '>"</span><span style='color:#808030; '>]</span> <span style='color:#808030; '>:</span> String<span style='color:#808030; '>.</span>Empty<span style='color:#800080; '>;</span>
    <span style='color:#800080; '>}</span>
<span style='color:#800080; '>}</span>
</pre>
</span>